package com.system.user.controller;

import com.system.common.utils.R;
import com.system.user.entity.LightingEntity;
import com.system.user.service.LightingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "电量表接口")
@RestController
@RequestMapping("/lighting")
public class LightingController {

    @Autowired
    private LightingService lightingService;

    /**
     * 查询所有电表数据
     *
     * @return
     */
    @ApiOperation(value = "查询所有电表数据")
    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public R findAll() {
        List<LightingEntity> lighting = lightingService.findAll();
        return R.ok().put("lighting", lighting);
    }

    /**
     * 模糊查询所有电表数据
     *
     * @param lightingEntity
     * @return
     */
    @ApiOperation(value = "模糊查询所有电表数据")
    @RequestMapping(value = "/findBySiteOrNumber", method = RequestMethod.POST)
    public R findBySiteOrNumber(@RequestBody LightingEntity lightingEntity) {
        List<LightingEntity> lighting = lightingService.findBySiteOrNumber(lightingEntity);
        return R.ok().put("lighting", lighting);
    }

    /**
     * 新增电表数据
     *
     * @param lighting
     * @return
     */
    @ApiOperation(value = "新增电表数据")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R add(@RequestBody LightingEntity lighting) {
        try {
            boolean is_success = lightingService.add(lighting);
            if (is_success) {
                return R.ok("添加成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("添加失败！");
        }
        return R.error("添加失败！");
    }

    /**
     * 更新电表数据
     *
     * @param lighting
     * @return
     */
    @ApiOperation(value = "更新电表数据")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public R update(@RequestBody LightingEntity lighting) {
        boolean is_success = lightingService.update(lighting);
        if (is_success) {
            return R.ok("修改成功！");
        }
        return R.error("修改失败！");
    }

    /**
     * 删除电表数据
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "删除电表数据")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public R delete(@RequestParam("id") Integer id) {
        boolean is_success = lightingService.delete(id);
        if (is_success) {
            return R.ok("删除成功！");
        }
        return R.error("删除失败！");
    }
}
